#pragma once
#include "cocos2d.h"
#include "cocos-ext.h"
#include "Hero.h"
USING_NS_CC;
USING_NS_CC_EXT;
using namespace gui;
class MainScene:public CCScene
{
public:

	MainScene(void)
	{
		this->initScene();
		this->schedule(schedule_selector(MainScene::frame),0.03f);
	}
private:
	CCArray* battleArray;
	Hero* hero;
	void touchEvent(CCObject* pSender,cocos2d::gui::TouchEventType type){
		UIButton*  button=(UIButton*)pSender;
		switch(type){
		case TOUCH_EVENT_BEGAN:
			if(strcmp(button->getName(),"left")==0){
				hero->setState(XRUN);
				hero->setDirection(LEFT);
			}else if(strcmp(button->getName(),"right")==0){
				hero->setState(XRUN);
				hero->setDirection(RIGHT);
			}else if(strcmp(button->getName(),"up")==0){
				hero->setState(YRUN);
				hero->setDirection(UP);
			}else if(strcmp(button->getName(),"down")==0){
				hero->setState(YRUN);
				hero->setDirection(DOWN);
			}
			break;
		case TOUCH_EVENT_ENDED:
			hero->setState(STAND);
			break;
		}
	}
	void initScene(){
		battleArray=new CCArray();

		CCNode *node=SceneReader::sharedSceneReader()->createNodeWithSceneFile("xyz.json");
		this->addChild(node);

		CCNode *pMapNode = (CCNode*)(node->getChildByTag(10003));
		CCComRender *render=(CCComRender*)pMapNode->getChildByTag(10004)->getComponent("CCArmature");
		CCArmature *pArmature = (CCArmature*)(render->getNode());

		hero=new Hero(15,10,pArmature,STAND,RIGHT);
		battleArray->addObject(hero);
		CCLOG("battleArray->addObject(hero);%d",battleArray->count());

		CCComRender *mapRender = (CCComRender*)pMapNode->getComponent("CCTMXTiledMap");
		CCTMXTiledMap* map=(CCTMXTiledMap*)mapRender->getNode();
		CCTMXLayer* layer1=map->layerNamed("layer1");
		layer1->setTileGID(1,ccp(2,2));
		layer1->setTileGID(1,ccp(3,3));


		CCComRender *renderGUIComponent=(CCComRender*)node->getChildByTag(10008)->getComponent("GUIComponent");
		TouchGroup *touchGroup = (TouchGroup*)(renderGUIComponent->getNode());
		UIWidget* widget =	(UIWidget*)touchGroup->getWidgetByName("controller");
		UIButton*  left=(UIButton*)widget->getChildByName("left");
		UIButton*  right=(UIButton*)widget->getChildByName("right");
		UIButton*  up=(UIButton*)widget->getChildByName("up");
		UIButton*  down=(UIButton*)widget->getChildByName("down");
		left->addTouchEventListener(this,toucheventselector(MainScene::touchEvent));
		right->addTouchEventListener(this,toucheventselector(MainScene::touchEvent));
		up->addTouchEventListener(this,toucheventselector(MainScene::touchEvent));
		down->addTouchEventListener(this,toucheventselector(MainScene::touchEvent));
	}
	void  frame(float f){
		CCObject* obj = NULL;
		CCARRAY_FOREACH(battleArray, obj){
			BattleObject* battleObject=(BattleObject*)obj;
			battleObject->frame();
		}
	}
};

